<template>
  <div class="roles-container">
    <el-card class="roles-card">
      <template #header>
        <div class="card-header">
          <span>角色管理</span>
          <el-button type="primary" @click="handleAdd">新增角色</el-button>
        </div>
      </template>
      
      <el-table :data="roleList" style="width: 100%" v-loading="loading">
        <el-table-column prop="name" label="角色名称" />
        <el-table-column prop="code" label="角色编码" />
        <el-table-column prop="description" label="描述" show-overflow-tooltip />
        <el-table-column prop="createTime" label="创建时间" />
        <el-table-column label="操作" width="250">
          <template #default="{ row }">
            <el-button type="primary" link @click="handleEdit(row)">编辑</el-button>
            <el-button type="primary" link @click="handlePermission(row)">权限配置</el-button>
            <el-button type="primary" link @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="total"
          layout="total, sizes, prev, pager, next"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 权限配置对话框 -->
    <el-dialog
      v-model="permissionDialogVisible"
      title="权限配置"
      width="600px"
    >
      <el-tree
        ref="permissionTree"
        :data="permissionList"
        show-checkbox
        node-key="id"
        :props="{
          children: 'children',
          label: 'name'
        }"
      />
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="permissionDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSavePermissions">
            确认
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import type { ElTree } from 'element-plus'

interface Role {
  id: number
  name: string
  code: string
  description: string
  createTime: string
}

interface Permission {
  id: number
  name: string
  children?: Permission[]
}

const loading = ref(false)
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
const roleList = ref<Role[]>([])
const permissionDialogVisible = ref(false)
const permissionTree = ref<InstanceType<typeof ElTree>>()

// 模拟数据
const mockRoles: Role[] = [
  {
    id: 1,
    name: '系统管理员',
    code: 'ADMIN',
    description: '系统最高权限管理员',
    createTime: '2024-01-01 00:00:00'
  },
  {
    id: 2,
    name: 'HR管理员',
    code: 'HR_ADMIN',
    description: '人力资源管理员',
    createTime: '2024-01-01 00:00:00'
  }
]

const permissionList: Permission[] = [
  {
    id: 1,
    name: '系统管理',
    children: [
      { id: 11, name: '用户管理' },
      { id: 12, name: '角色管理' },
      { id: 13, name: '部门管理' }
    ]
  },
  {
    id: 2,
    name: '员工管理',
    children: [
      { id: 21, name: '员工档案' },
      { id: 22, name: '员工考核' }
    ]
  }
]

function loadRoles() {
  loading.value = true
  // TODO: 替换为实际的 API 调用
  setTimeout(() => {
    roleList.value = mockRoles
    total.value = mockRoles.length
    loading.value = false
  }, 500)
}

function handleAdd() {
  // TODO: 实现新增角色功能
  ElMessage.info('新增角色功能开发中')
}

function handleEdit(row: Role) {
  // TODO: 实现编辑角色功能
  ElMessage.info('编辑角色功能开发中')
}

function handlePermission(row: Role) {
  permissionDialogVisible.value = true
  // TODO: 加载该角色的权限数据
}

function handleSavePermissions() {
  if (!permissionTree.value) return
  
  const checkedKeys = permissionTree.value.getCheckedKeys()
  const halfCheckedKeys = permissionTree.value.getHalfCheckedKeys()
  
  // TODO: 保存权限配置
  console.log('选中的权限:', checkedKeys)
  console.log('半选中的权限:', halfCheckedKeys)
  
  ElMessage.success('权限配置保存成功')
  permissionDialogVisible.value = false
}

function handleDelete(row: Role) {
  ElMessageBox.confirm(
    '确定要删除该角色吗？',
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
      // TODO: 实现删除角色功能
      ElMessage.success('删除成功')
    })
    .catch(() => {
      ElMessage.info('已取消删除')
    })
}

function handleSizeChange(val: number) {
  pageSize.value = val
  loadRoles()
}

function handleCurrentChange(val: number) {
  currentPage.value = val
  loadRoles()
}

// 初始加载
loadRoles()
</script>

<style scoped>
.roles-container {
  padding: 20px;
}

.roles-card {
  background: var(--el-bg-color);
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  margin-top: 20px;
}
</style>